package app.netant.finishbook.v3books;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class V3BooksDetail extends NetAntBase {
	
	private V3BooksEntity entity;
	
	private String exp = "";

	public V3BooksDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(V3BooksEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select(".main #content .info .infotop small a");
		if(activePart.size()>0){
			String type = activePart.get(0).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public void analyseTitle(){
		Elements activePart = document.select(".main #content .info .info_1 .infotitle h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".txt");
		}
	}
	
	public void analyseWriter(){
		Elements activePart = document.select(".main #content .info .info_1 .infotitle span");
		if(activePart.size()>0){
			String writers = activePart.get(0).text();
			int idx = writers.indexOf("：");
			if(idx>0){
				writers = writers.substring(idx+1).trim();
			}
			System.out.println("【Writer】:"+writers);
			this.entity.setNovelWriter(writers);
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select(".main #content .info .jianjie");
		if(activePart.size()>0){
			
			String tmp = activePart.get(0).text();
			int idx1 = tmp.indexOf("[关 键 词]：");
			int idx2 = tmp.indexOf("[本文地址]：");
			
			String memo = tmp;
			if(idx1>0){
				memo = tmp.substring(0,idx1);		
			}else if(idx2>0){
				memo = tmp.substring(0,idx2);		
			}
			
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}
	
	public void analyseDate(){
		Elements activePart = document.select(".main #content .info .jianjie .new");
		if(activePart.size()>0){
			
			String str = activePart.get(0).text();
			int idx = str.indexOf("最新更新：");
			if(idx>0){
				String date = str.substring(idx+5).trim();
				System.out.println("【Date】："+date);
				this.entity.setNovelDate(date);
			}
			
		}
	}
	
	public void analyseHref(){
		Elements activePart = document.select(".main #content .info .read a");
		for(int i=0;i<activePart.size();i++){
			String text = activePart.get(i).text();
			if(text != null){
				text = text.replaceAll(" ", "");
			}
			if("TXT下载".equals(text)){
				String href = activePart.get(i).attr("href");
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				break;
			}
		}
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseWriter();
		this.analyseMemo();
		this.analyseDate();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		V3BooksDetail detail = new V3BooksDetail("http://www.3vbooks.com/book/41/41635.htm");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseWriter();
		detail.analyseMemo();
		detail.analyseDate();
		detail.analyseHref();
	}
	
}
